package com.shushan.home.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shushan.home.bean.RecordBean;
import com.shushan.home.bean.TotalPlayBean;
import com.shushan.shushanbao.R;
import com.shushan.util.DateUtils;
import com.shushan.util.ToolUtils;

import java.util.List;

/**
 * @ClassName: AppUseTimeAdapter
 * @Desciption: //APP使用时间适配器
 * @author: zhangshihao
 * @date: 2018-09-19
 */
public class AppUseTimeAdapter extends RecyclerView.Adapter<AppUseTimeAdapter.ViewHolder>{

    private static final String TAG = AppUseTimeAdapter.class.getSimpleName();

    private List<RecordBean.TodayPlayListBean> playListBeans;
    private int totalPlayTime = 0;//分钟

    public AppUseTimeAdapter(List<RecordBean.TodayPlayListBean> playListBeans,int totalPlayTime){
        this.playListBeans = playListBeans;
        this.totalPlayTime = totalPlayTime;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app_use_time,parent,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if(playListBeans == null){
            return;
        }
        if(playListBeans != null && playListBeans.size() > 0) {
            RecordBean.TodayPlayListBean playListBean = playListBeans.get(position);
            if (totalPlayTime > 0) {
                int percent = (int) (((float) playListBean.duration / (totalPlayTime * 60)) * 100);
                holder.progressBar.setProgress(percent);
            } else {
                holder.progressBar.setProgress(0);
            }
            holder.tvAppName.setText(ToolUtils.getPreThreeStr(playListBean.app_name));
            int time;
            float useTime = (float)playListBean.duration/60;
            if(useTime > 0 && useTime < 1){
                time = 1;
            }else{
                time = (int)useTime;
            }
            holder.tvUseTime.setText(time + "分钟");
        }
    }

    @Override
    public int getItemCount() {
        return playListBeans.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private ProgressBar progressBar;
        private TextView tvAppName,tvUseTime;
        public ViewHolder(View itemView) {
            super(itemView);
            progressBar = itemView.findViewById(R.id.pb_use_time);
            tvAppName = itemView.findViewById(R.id.tv_app_name);
            tvUseTime = itemView.findViewById(R.id.tv_use_time);
        }
    }

}
